/**
 * @author: zzj
 * @date: 2020-03-28 10:46:58
 * @version: 1.0
 */
import Vue from 'vue';
import {Validate} from 'js-utils/validate';

/**
 *
 * @param file {File}
 * @param size {Number}
 * @param type {String}
 * @returns {boolean}
 */
export function checkImg(file, size = 5, type = 'jpeg|jpg|png') {
  let limitType = new RegExp(`^image\\/(${type})$`, 'ig').test(file.type);
  let limitSize = file.size / 1024 / 1024 < size;
  if (!limitType) {
    Vue.prototype.$message.error(`图片只能是 ${type} 格式!`);
    return false;
  }
  if (!limitSize) {
    let sizeTips = size + 'MB';
    if (size < 1) {
      sizeTips = Math.round(size * 1024) + 'KB';
    }
    Vue.prototype.$message.error(`图片大小不能超过 ${sizeTips}!`);
    return false;
  }
  return true;
}

export function getResult(val, ...args) {
  return Validate.isFunction(val) ? val(...args) : val;
}

export function getInstance(component, options) {
  let Constructor = Vue.extend(component);
  let instance = new Constructor(options);
  instance.$mount();
  document.body.appendChild(instance.$el);
  return instance;
}
